/**
 * 多模型适配器基类
 * 基于Story 3.2的多模型接口适配层设计
 */

export interface ModelConfig {
  apiKey: string;
  baseUrl?: string;
  maxTokens: number;
  temperature: number;
  timeout: number;
  retryAttempts: number;
}

export interface ModelResponse {
  content: string;
  usage: {
    promptTokens: number;
    completionTokens: number;
    totalTokens: number;
  };
  model: string;
  finishReason: 'stop' | 'length' | 'content_filter' | 'error';
  cost?: number;
}

export interface StreamResponse {
  content: string;
  done: boolean;
  usage?: ModelResponse['usage'];
}

export abstract class BaseModelAdapter {
  protected config: ModelConfig;
  protected modelName: string;

  constructor(config: ModelConfig, modelName: string) {
    this.config = config;
    this.modelName = modelName;
  }

  /**
   * 生成文本内容
   */
  abstract generate(prompt: string, options?: Partial<ModelConfig>): Promise<ModelResponse>;

  /**
   * 流式生成文本内容
   */
  abstract stream(
    prompt: string, 
    onChunk: (chunk: StreamResponse) => void,
    options?: Partial<ModelConfig>
  ): Promise<void>;

  /**
   * 计算token数量
   */
  abstract countTokens(text: string): Promise<number>;

  /**
   * 估算成本
   */
  abstract estimateCost(promptTokens: number, completionTokens: number): number;

  /**
   * 健康检查
   */
  abstract healthCheck(): Promise<boolean>;

  /**
   * 获取模型信息
   */
  getModelInfo() {
    return {
      name: this.modelName,
      maxTokens: this.config.maxTokens,
      temperature: this.config.temperature
    };
  }
}

/**
 * 模型类型枚举
 */
export enum ModelType {
  GPT4 = 'gpt-4',
  GPT35 = 'gpt-3.5-turbo',
  CLAUDE = 'claude-3-sonnet',
  WENXIN = 'ernie-bot-4',
  QWEN = 'qwen-max',
  SPARK = 'spark-3.5',
  KIMI = 'moonshot-v1-8k',
  DEEPSEEK = 'deepseek-chat',
  DOUBAO = 'doubao-pro-4k',
  HUNYUAN = 'hunyuan-pro'
}

/**
 * 模型能力配置
 */
export const MODEL_CAPABILITIES = {
  [ModelType.GPT4]: {
    maxTokens: 8192,
    languages: ['en', 'zh', 'ja', 'ko'],
    specialties: ['creative', 'technical', 'analysis'],
    costPer1kTokens: 0.03,
    provider: 'OpenAI'
  },
  [ModelType.GPT35]: {
    maxTokens: 4096,
    languages: ['en', 'zh'],
    specialties: ['general', 'conversation'],
    costPer1kTokens: 0.002,
    provider: 'OpenAI'
  },
  [ModelType.CLAUDE]: {
    maxTokens: 100000,
    languages: ['en', 'zh'],
    specialties: ['analysis', 'reasoning', 'creative'],
    costPer1kTokens: 0.015,
    provider: 'Anthropic'
  },
  [ModelType.WENXIN]: {
    maxTokens: 4096,
    languages: ['zh', 'en'],
    specialties: ['chinese', 'general'],
    costPer1kTokens: 0.012,
    provider: 'Baidu'
  },
  [ModelType.QWEN]: {
    maxTokens: 6000,
    languages: ['zh', 'en'],
    specialties: ['chinese', 'technical'],
    costPer1kTokens: 0.008,
    provider: 'Alibaba'
  }
};
